/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.uah.es.snomedctsubsetextractor;

import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Luis
 */
public class SNOMEDUtil {

    private SNOMEDCTConfigurator conf;

    public SNOMEDUtil(SNOMEDCTConfigurator conf) {
        this.conf = conf;
    }

    public LinkedList<Integer> getConceptId(String concept) {

        String sql;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        
        LinkedList<Integer> id = new LinkedList<Integer>();

        if (this.conf.getValue("sgbd").compareToIgnoreCase("mysql") == 0) {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql:" + this.conf.getValue("url") + this.conf.getValue("schema"), this.conf.getValue("user"), this.conf.getValue("password"));

                //sql = "SELECT conceptid FROM concepts c where c.fullyspecifiedname like '%" + concept + "%'";
                sql = "SELECT conceptid FROM descriptions d where d.`TERM` = '" + concept + "' and DESCRIPTIONSTATUS = 0";
                //System.out.println("Consulta ejecutada: " + sql);
                stmt = conn.prepareStatement(sql);

                rs = stmt.executeQuery();

                while (rs.next()) {
                    id.add(rs.getInt("conceptid"));
                }
                
                //System.out.println("Para el concepto: " + concept +" se han recuperado: " + id.size() + " terminos.");

                rs.close();
                stmt.close();
                conn.close();

            } catch (SQLException ex) {
                Logger.getLogger(SNOMEDCTDatabase.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(SNOMEDCTDatabase.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(SNOMEDCTDatabase.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SNOMEDCTDatabase.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                return id;
            }
        } else {
            throw new UnsupportedOperationException("Not supported yet.");
        }

    }
    
    public SNOMEDConcept getSNOMEDConcept (Integer id){
        
        SNOMEDConcept concept = null;
        
        String sql;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        if (this.conf.getValue("sgbd").compareToIgnoreCase("mysql") == 0) {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql:" + this.conf.getValue("url") + this.conf.getValue("schema"), this.conf.getValue("user"), this.conf.getValue("password"));

                //sql = "SELECT conceptid FROM concepts c where c.fullyspecifiedname like '%" + concept + "%'";
                sql = "SELECT fullyspecifiedname FROM concepts where conceptid = " + id + " and CONCEPTSTATUS = 0";
                //System.out.println("Consulta ejecutada: " + sql);
                stmt = conn.prepareStatement(sql);

                rs = stmt.executeQuery();
                
                rs.first();
                concept = new SNOMEDConcept(id, rs.getString("fullyspecifiedname"));

                rs.close();
                stmt.close();
                conn.close();

            } catch (SQLException ex) {
                Logger.getLogger(SNOMEDCTDatabase.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(SNOMEDCTDatabase.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(SNOMEDCTDatabase.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SNOMEDCTDatabase.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                return concept;
            }
        } else {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        
        
        
    }
}
